package com.cqu.lpss.controller;

import com.cqu.lpss.mapper.ImageMapper;
import com.cqu.lpss.pojo.Image;
import com.cqu.lpss.pojo.PageBean;
import com.cqu.lpss.pojo.Result;
import com.cqu.lpss.service.impl.ImageImpl;
import com.cqu.lpss.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/image")
public class ImageController {
    @Autowired
    AliOSSUtils aliOSSUtils;
    @Autowired
    ImageImpl imageImpl;
    @PostMapping
    public Result list(@RequestParam(defaultValue = "1")int page,
                       @RequestParam(defaultValue = "10")int pageSize,
                        String imgUrl,
                        String imgPosition,
                        String imgFormat,
                        String imgName){
        log.info("params:{}",imgName);
        PageBean<Image> pageBean =imageImpl.list(page, pageSize, imgUrl, imgPosition, imgFormat, imgName);
        return Result.success(pageBean);

    }
    @PostMapping("/upload")
    public Result upload(@RequestPart(value = "file")MultipartFile uploadfile) throws IOException{
        log.info("文件：{}",uploadfile);
        log.info("文件上传，文件名：{}", uploadfile.getOriginalFilename());
        //阿里云工具类进行文件上传
        String url = aliOSSUtils.upload(uploadfile);
        log.info("文件上传完成，文件访问的url:{}",url);
        return Result.success(url);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Image image){
        log.info("图片信息：{}",image);
        int ans = imageImpl.add(image);
        if(ans==1){
            return Result.success();
        }else {
            return Result.erro("NOT_SUCCESS");
        }
    }
    @DeleteMapping("/delete/{imgId}")
    public Result delete(@PathVariable Integer imgId){
        int res = imageImpl.delete(imgId);
        if(res==1){
            return Result.success();
        }else {
            return Result.erro("ERROR");
        }
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody Image image){
        log.info("image:{}",image);
        int res = imageImpl.edit(image);
        if(res==1){
            return Result.success();
        }else {
            return Result.erro("ERROR");
        }
    }
    @PostMapping("/findByposition")
    public Result findByposition(@RequestParam String position){
        return Result.success(imageImpl.findByposition(position));
    }
}
